/* File name: BufferedFPGALoopBack.c

	08Dec2007 .. initial version .. KMetzger
	             
*/
#include <stdio.h>


void McBSP0open(int *, unsigned, int *, unsigned, unsigned);
void McBSP0_Put(unsigned, unsigned);
void McBSP0_Get(unsigned *, unsigned *);

int DATA[100];

#define Npairs 100
unsigned InBuffer[2*Npairs], OutBuffer[2*Npairs];
unsigned x,y;

void main(void)
{
	long i,k;
	unsigned loop=1;
	unsigned short tmp;

	FILE *fid;

/*	FILE *R;
	FILE *G;
	FILE *B;*/

	fid = fopen("pimpinData.txt","w");
/*
	R = fopen("imageR.txt","w");
	if (R==NULL) {
		printf("can't open output\n");
		exit(1);
	}
	G = fopen("imageG.txt","w");
	if (G==NULL) {
		printf("can't open output\n");
		exit(1);
	}
	B = fopen("imageB.txt","w");
	if (B==NULL) {
		printf("can't open output\n");
		exit(1);
	}*/


	McBSP0_Open(InBuffer, Npairs, OutBuffer, Npairs, 0);
	_enable_interrupts();

	while(loop) {
		
		for( i=0; i<100; i++ )	// number of rows
		{
		/*	for ( k=0; k<356; k++)	// number of columns
			{
				McBSP0_Get(&x, &y);	// receive a pair of 16-bit words from FPGA
				tmp = (y>>5)&0x7;
				fprintf(R, "%2u", tmp);
				tmp = (y>>2)&0x7;
				fprintf(G, "%2u", tmp);
				tmp = y&0x3;
				fprintf(B, "%2u", tmp);
			}
			fprintf(R, "\n");
			fprintf(G, "\n");
			fprintf(B, "\n"); */

			McBSP0_Get(&x,&y);
			DATA[i] = x;

			McBSP0_Put(x, y);	// send back the same data to FPGA
		}
		loop=0;
	}
	printf("done receiving frame\n");

	for (k=0; k<100;k++){
		fprintf(fid, "%u\n", DATA[k]);
	}
	printf("done pimpin'\n");

	
	fclose(fid);
}
